package com.ymatou.socialbizer.testcase;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.socialbizer.parameter.SellerStoriesBean;
import com.ymatou.socialbizer.service.SellerStoriesCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P3;
import com.ymttest.database.mongo.NoteMongoWapper;



public class Ts_SellerStories {
	private static SellerStoriesBean sellerstoriesBean;
	private static SellerStoriesCall sellerstoriesCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买手生活故事接口");
	}

	@Before
	public void caseUp() {
		sellerstoriesBean=new SellerStoriesBean();
		sellerstoriesCall=new SellerStoriesCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellerstoriesCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SellerStories_001() {
		Logger.start(true,"买手生活故事-验证接口返回公共数据 ");
		try {
			//设置参数
			
			sellerstoriesBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,addtime");
			sellerstoriesBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			sellerstoriesBean.setLimit("0,10");
			
			sellerstoriesCall.setData(sellerstoriesBean);
			sellerstoriesCall.callService();	
			
			JSONArray hits = sellerstoriesCall.getHits();
			
			Logger.verifyEquals("OK",sellerstoriesCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(sellerstoriesCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, sellerstoriesCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, sellerstoriesCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyNotNull(sellerstoriesCall.getCommonInfo("requestid"), "requestid不为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SellerStories_002() {
		Logger.start(true,"买手生活故事-验证接口返回数据是否重复 ");
		try {
			//设置参数
			
			sellerstoriesBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,addtime");
			sellerstoriesBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			sellerstoriesBean.setLimit("0,10");
			
			sellerstoriesCall.setData(sellerstoriesBean);
			sellerstoriesCall.callService();	
			
			JSONArray hitsAry = sellerstoriesCall.getHits();
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String noteid = json.getString("noteid");
				
				if (list.contains(noteid) == true) {

					Logger.verifyEquals(false, list.contains(noteid), "验证第" + i+ "笔记id为" + noteid+"的买手是否存在重复数据");

				}else{
					Logger.verifyEquals(false, list.contains(noteid), "验证第" + i+ "笔记id为" + noteid+"的买手是否存在重复数据");
					list.add(noteid);
				}
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SellerStories_003() {
		Logger.start(true,"买手生活故事-验证数据是否按照添加的时间排序 ");
		try {
			//设置参数
			
			sellerstoriesBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,addtime");
			sellerstoriesBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			sellerstoriesBean.setLimit("0,10");
			
			sellerstoriesCall.setData(sellerstoriesBean);
			sellerstoriesCall.callService();	
			
			JSONArray hitsAry = sellerstoriesCall.getHits();
			DateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
			
			for (int i = 1; i <=sellerstoriesCall.getHits().length(); i++) {
				for (int j = i+1; j <= sellerstoriesCall.getHits().length(); j++) {
					
					String noteInfo1 = sellerstoriesCall.getHits().get(i - 1).toString();
					JSONObject noteInfoJson1 = new JSONObject(noteInfo1);
					String addtime1 =   (String) noteInfoJson1.get("addtime");
					
					String noteInfo2 = sellerstoriesCall.getHits().get(j - 1).toString();
					JSONObject noteInfoJson2 = new JSONObject(noteInfo2);
					String addtime2=  (String) noteInfoJson2.get("addtime");
					
					 Date date1 = time.parse(addtime1 );
					 Date date2 = time.parse(addtime2 );

					Logger.verifyEquals(true, date1.getTime()>date2.getTime(), "检查排在第"+i+"位的笔记添加时间为"+date1+"，比排在第"+j+"位的笔记添加时间值为:"+date2+"的靠前");
				}
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SellerStories_004() {
		Logger.start(true,"买手生活故事-检查接口返回数据信息是否完整 ");
		try {
			//设置参数
			
			sellerstoriesBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,addtime");
			sellerstoriesBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			sellerstoriesBean.setLimit("0,10");
			
			sellerstoriesCall.setData(sellerstoriesBean);
			sellerstoriesCall.callService();	
			
			JSONArray hits = sellerstoriesCall.getHits();
			
			for(int i =1;i<=hits.length();i++){
				String ids = hits.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				int userid = json.getInt("userid");
				int noteid = json.getInt("noteid");
				int commentnum = json.getInt("commentnum");
				int collectionnum = json.getInt("collectionnum");
				String content = json.getString("content");
				JSONArray pics =  (JSONArray) json.get("pics");
				
				Logger.verifyNotNull(userid, "userid-买手id值为必须返回字段值");
				Logger.verifyNotNull(noteid, "noteid-笔记id值为必须返回值");
				Logger.verifyNotNull(commentnum, "commentnum-评论数为必须返回值");
				Logger.verifyNotNull(collectionnum, "collectionnum-点赞数为必须返回值");
				Logger.verifyNotNull(content, "content-笔记内容值为必须返回字段值");
				Logger.verifyEquals(true, pics.length()>0, "pics-图片路径为必须返回值");
				
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SellerStories_005() {
		Logger.start(true,"买手生活故事-验证接口返回内容是否与数据库中一致 ");
		try {
			//设置参数
			
			sellerstoriesBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,addtime");
			sellerstoriesBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			sellerstoriesBean.setLimit("0,10");
			
			sellerstoriesCall.setData(sellerstoriesBean);
			sellerstoriesCall.callService();	
			
			JSONArray hits = sellerstoriesCall.getHits();
			
			NoteMongoWapper notes = new NoteMongoWapper();
			//连接mongo通过noteid查出对应笔记的内容
			for(int i =1;i<=sellerstoriesCall.getHits().length();i++){
				String ids = hits.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				int noteid = json.getInt("noteid");
				
				//连接mongo查询对应笔记是否为小编推荐
				Map noteInfo = notes.getNoteInfoByNoteId(noteid);
				Map NewNoteInfoObject =   (Map) noteInfo.get("OldNoteInfo");
				String Content = (String) NewNoteInfoObject.get("Content");
				
				Logger.verifyEquals(Content,json.get("content"), "检查noteid为："+noteid+"的 第"+i+"条笔记内容是否和数据库中的一致");
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SellerStories_006() {
		Logger.start(true,"买手生活故事-接口返回笔记是否所属买手 ");
		try {
			//设置参数
			
			sellerstoriesBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,addtime");
			sellerstoriesBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			sellerstoriesBean.setLimit("0,10");
			
			sellerstoriesCall.setData(sellerstoriesBean);
			sellerstoriesCall.callService();	
			
			NoteMongoWapper notes = new NoteMongoWapper();
			
			for(int i =1;i<=sellerstoriesCall.getHits().length();i++){
				String ids = sellerstoriesCall.getHits().get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				int noteid = json.getInt("noteid");
				
				//连接mongo查询对应笔记是否为小编推荐
				Map noteInfo = notes.getNoteInfoByNoteId(noteid);
				
				Logger.verifyEquals(3,noteInfo.get("NoteSource") , "检查noteid为："+noteid+"的 第"+i+"条笔记是否为买手笔记");
				
			}
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SellerStories_007() {
		Logger.start(true,"买手生活故事-验证接口分页");
		try {
			//设置参数
			int limit = 4;
			
			sellerstoriesBean.setFl("userid,pics,noteid,collectionnum,content,commentnum,addtime");
			sellerstoriesBean.setInfo("userid:111,cookieid:111555555555,yid:abcde");
			sellerstoriesBean.setLimit("0,"+4+"");
			
			sellerstoriesCall.setData(sellerstoriesBean);
			sellerstoriesCall.callService();	
			
			NoteMongoWapper notes = new NoteMongoWapper();
			if(limit>sellerstoriesCall.getHits().length()){
				limit = sellerstoriesCall.getHits().length();
			}
			
			Logger.verifyEquals(limit, sellerstoriesCall.getHits().length(), "验证接口分页返回数据数目是否正确");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	//TODO 查询mongo查出所有满足要求的记录与接口返回记录数对比
	
}
